package com.yupi.usercenter.listener;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.yupi.usercenter.common.ErrorCode;
import com.yupi.usercenter.exception.BusinessException;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        registry.addInterceptor(new SaInterceptor(
                headler -> {
                    if (!StpUtil.isLogin()) {
                        throw new BusinessException(ErrorCode.NOT_LOGIN);
                    }
                }
                ))
                // 拦截所有路径
                .addPathPatterns("/**")
                // 不拦截的路径
                .excludePathPatterns("/user/login", "/user/register","/swagger-ui.html","/swagger-resources/**","/v2/api-docs","/webjars/**");
    }
}
